﻿using Joker.Model;
using Joker.View.Editor;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View.Management {
    public partial class UserManage : Form {
        private static string sqlStr = "SELECT * FROM [User]";
        private bool Search = true;
        public UserManage() {
            InitializeComponent();
        }

        private void UserManage_Load(object sender, EventArgs e) {
            comboBox1.SelectedIndex = 0;
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlStr);
        }

        private void button1_Click(object sender, EventArgs e) {
            if (Search) {
                Search = false;
                button1.Text = "重置";
                dataGridView1.DataSource = DBHelper.GetDataTable($"{sqlStr} WHERE [Name] LIKE '%{textBox1.Text}%' AND [Gender] = '{(comboBox1.SelectedItem.Equals("男") ? "true" : "false")}' AND [Tel] LIKE '%{textBox2.Text}%'");
            } else {
                Search = true;
                button1.Text = "查询";
                dataGridView1.DataSource = DBHelper.GetDataTable(sqlStr);
            }
        }


        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
            if (e.RowIndex > 0 && e.RowIndex < dataGridView1.Rows.Count) dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[0];
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e) {
            OpenForm.NewForm(this, new UserManageEdit(), false);
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlStr);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) {
            UserManageEdit dataEdit = new UserManageEdit();
            dataEdit.ID = (int)dataGridView1.CurrentRow.Cells[0].Value;
            OpenForm.NewForm(this, dataEdit, false);
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlStr);
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) {
            if (MessageBox.Show($"确认删除ID为{dataGridView1.CurrentRow.Cells[0].Value}的数据","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes) {
                if (DBHelper.ExecuteNonQuery($"DELETE [User] WHERE [ID] = {dataGridView1.CurrentRow.Cells[0].Value}")) {
                    MessageBox.Show("删除成功!");
                } else {
                    MessageBox.Show("删除失败!");
                }
            }
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlStr);
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
            if (e.ColumnIndex == 1) {
                if (e.Value != null && e.Value.ToString().Length > 0) {
                    e.Value = TextData.Debase64(e.Value.ToString());
                }
            } else if (e.ColumnIndex == 2) {
                if (e.Value != null && e.Value.ToString().Length > 0) {
                    e.Value = new string('*', e.Value.ToString().Length);
                }
            }
        }
    }
}
